package com.nursinghome.common.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.nursinghome.bean.PageFilter;
import com.nursinghome.bean.Service;
import com.nursinghome.common.mapper.ServiceMapper;
import com.nursinghome.common.service.CategoryService;
import com.nursinghome.common.service.ServiceService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
* @author 程硕
* @createDate 2025-04-07 01:10:33
*/
@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService{
    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private CategoryService categoryService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insert(Service service) throws NurisingHomeException {
        // 判断是否有同名服务
        List<Service> list = serviceMapper.selectAll(null,null ,
                new Service() {{
                    setName(service.getName());
                }}
        );
        Service serviceByName = ObjectUtil.isEmpty(list) ? null : list.get(0);
        if (ObjectUtil.isNotEmpty(serviceByName)){
            throw new NurisingHomeException("服务已存在");
        }
        return serviceMapper.insert(service) > 0;
    }

    @Override
    public Boolean update(Service service) throws NurisingHomeException {
        // 判断服务是否存在
        // 判断修改之后是否存在同名服务
        Service serviceById = serviceMapper.selectById(service.getId());
        if (ObjectUtil.isEmpty(serviceById)){
            throw new NurisingHomeException("服务不存在");

        }
        List<Service> list = serviceMapper.selectAll(null,null ,new Service() {{
            setName(service.getName());
        }});
        Service serviceByName = ObjectUtil.isEmpty(list) ? null : list.get(0);
        if (    serviceByName != null
                && ObjectUtil.isNotEmpty(serviceByName)
                && !Objects.equals(serviceByName.getId(), service.getId())) {
            throw new NurisingHomeException("服务已存在");
        }
        return serviceMapper.update(service) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Integer id) throws NurisingHomeException {
        // 判断是否有这个服务
        Service serviceById = serviceMapper.selectById(id);
        if (ObjectUtil.isEmpty(serviceById)){
            throw new NurisingHomeException("服务不存在，无法删除");
        };
        Service service = new Service() {{
            setId(id);
            setIsDelete(0);
        }};
        return serviceMapper.update(service) > 0;
    }

    @Override
    public Service selectById(Integer id) {
        return serviceMapper.selectById(id);
    }

    @Override
    public PageFilter<Service> selectAll(String pageNum, String pageSize,String prop ,String order , Service service) {
        service.setPageNum(pageNum);
        service.setPageSize(pageSize);
        List<Service> list = serviceMapper.selectAll(prop , order ,service);
        return service.getPageFilter(service, list);
    }
}
